<template>
    <div class="wy-form">
        <el-form v-if="!success" :model="ruleForm" :rules="rules" ref="ruleForm" label-position="top"
                 label-width="100px" class="demo-ruleForm">
            <h3>宁夏金河田物业-人脸识别门禁照片采集</h3>
            <el-form-item label="选择居住小区：" prop="community_id">
                <el-radio-group v-model="ruleForm.community_id">
                    <el-radio :key="item.community_id" v-for="item in communityList" :label="item.community_id">
                        {{item.community_name}}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="楼栋-单元-门号（xx-xx-xx）" prop="room_addr">
                <el-input v-model.trim="ruleForm.room_addr"/>
            </el-form-item>
            <el-form-item label="姓名" prop="full_name">
                <el-input v-model.trim="ruleForm.full_name"/>
            </el-form-item>
            <el-form-item label="照片（上传与个人姓名匹配的照片）" prop="head_id">
                <div class="upload-box">
                    <div v-if="fileList.length">
                        <div v-for="(file,index) in fileList" :key="file.name+index"
                             style="display:flex;justify-content: center">
                            <div class="img-box">
                                <img alt="图片" style="width: 100px;" :src="file.baseData"/>
                                <div class="tip" @click="()=>{fileList.splice(index,1)}"><i class="el-icon-close"/>
                                </div>
                            </div>
                        </div>
                    </div>
                    <label v-else>
                        <div class="img-box" style="margin: 0 auto">
                            <i class="el-icon-plus"/>
                        </div>
                        <input style="display: none" ref="imgFile" @change="setFiles" type="file"
                               accept="image/png,image/jpg,image/jpeg">
                    </label>
                </div>
            </el-form-item>
            <div class="footer">
                <el-button type="success" @click="submitForm('ruleForm')">提交</el-button>
                <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
            </div>
        </el-form>
        <div v-else style="padding: 50px 20px">
            <h1>{{' '}}</h1>
            <h1 style="text-align: center;color: #00ca00">提交成功！</h1>
            <h3 style="text-align: center">感谢您的配合</h3>
            <h5 style="text-align: center">————宁夏金和田物业</h5>
        </div>
    </div>
</template>
<script>
    import lrz from 'lrz'

    export default {
        data() {
            return {
                communityList: [],
                ruleForm: {
                    community_id: '',
                    room_addr: '',
                    full_name: '',
                    head_id: ''
                },
                rules: {
                    community_id: [
                        {required: true, message: '请选择居住小区', trigger: 'blur'},
                    ],

                    room_addr: [
                        {required: true, message: '请输入楼栋单元门号', trigger: 'blur'}
                    ],
                    full_name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    head_id: [
                        {required: true, message: '请上传本人纯底照片', trigger: 'blur'}
                    ]
                },
                fileList: [],
                success: false
            };
        },
        mounted() {
            this.getList()
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    const loading = this.$loading({
                        lock: true,
                        text: '信息提交中...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                    if (valid) {
                        this.$api.post('/api-upload-information', this.ruleForm, () => {
                            this.success = true;
                            loading.close()
                        }, () => {
                            loading.close()
                        })
                    }
                });
            },
            uploadImg() {
                let formdata = new FormData();
                this.fileList.forEach(item=>{
                    formdata.append('file', item.file)
                });
                const loading = this.$loading({
                    lock: true,
                    text: '图片上传中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                this.$api.post('/api-upload-head', formdata, (res) => {
                    this.ruleForm.head_id = res.file_id;
                    loading.close()
                }, () => {
                    loading.close()
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.fileList = [];
            },
            setFiles() {
                let files = this.$refs.imgFile.files;
                for (let i = 0; i < files.length; i++) {
                    let imgFile = files[i];
                    if (imgFile.size > 1024 * 5 * 1024) {
                        this.$toast.warning('上传照片大小不能大于3M');
                        break;
                    }
                    console.log(imgFile)
                    lrz(imgFile, {
                        width: 600,
                        filedName:imgFile.name
                    }).then((rst) => {
                        console.log(rst.file)
                        let resFile = new window.File([rst.file],imgFile.name,{ type: rst.file.type })
                        console.log('resFile',resFile)
                        this.fileList.push({name: imgFile.name, baseData: rst.base64,file:resFile});
                        this.uploadImg()
                        //成功时执行
                    }).catch((error) => {
                        this.$toast.error(JSON.stringify(error))
                        //失败时执行
                    })
                }
            },
            getList() {
                this.$api.get('/api-select-community', null, (res) => {
                    this.communityList = res;
                })
            }
        }
    }
</script>
<style lang="scss">
    .wy-form {
        .el-form {
            padding: 0 10px;
            display: block;
            width: 100%;
            height: 100%;
            overflow: auto;
            position: absolute;
            background: #f5f5f5;
        }

        .el-form-item {
            text-align: left;
            padding: 10px 5px 20px;
            background: #fefefe;
            border-radius: 10px;

            label {
                padding: 0 10px;
            }

            .el-form-item__error {
                padding-right: 20px;
                right: 0;
                left: auto;
            }
        }

        .el-radio-group {
            label.el-radio {
                display: block;
                padding: 5px 10px;
            }
        }

        .footer {
            text-align: center;
            padding-top: 10px;
            margin-bottom: 30px;

            button {
                width: 100px;
                margin: 0 20px;
            }
        }

        .upload-box {
            text-align: center;

            label {
                display: inline-block;

                i.el-icon-plus {
                    font-size: 40px;
                    color: #cccccc;
                }
            }
        }

        .img-box {
            width: 100px;
            min-height: 100px;
            position: relative;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #f9f9f9;

            .tip {
                position: absolute;
                background: rgba(225, 198, 178, 0.4);
                width: 16px;
                height: 16px;
                display: flex;
                justify-content: center;
                align-items: center;
                right: 0;
                top: 0;
                overflow: hidden;

                i::before {
                    color: #378dff;
                }

                i {
                    color: #cccccc;
                }
            }
        }
    }
</style>
